package model

import "time"

type StationB struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	Member bool
	ExpirationTime time.Time
	VideoCoverList []*VideoCover
	CommentMicroblog int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type VideoCover struct {
	Id int64 `xorm:"pk autoincr"`
	StationBId int64
	Name string
	VideoList []*Video
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Video struct {
	Id int64 `xorm:"pk autoincr"`
	VideoCoverId int64
	Name string
	Url string
	CommentList []*Comment
	CommentNumber int
	Fabulous bool `xorm:"-"`
	FabulousType int `xorm:"-"`
	Collection bool `xorm:"-"`
	Forward bool `xorm:"-"`
	FabulousNumber int
	ForwardNumber int
	CommentButton bool `xorm:"-"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Comment struct {
	Id int64 `xorm:"pk autoincr"`
	VideoId int64
	StationBId int64
	StationBName string
	Content string
	ReplyList []*Reply
	CreatedAt time.Time
	UpdatedAt time.Time
}

type Reply struct {
	Id int64 `xorm:"pk autoincr"`
	CommentId int64
	StationBId int64
	StationBName string
	ReceiveStationBId int64
	ReceiveStationBName string
	Content string
	CreatedAt time.Time
	UpdatedAt time.Time
}

type MemberPackage struct {
	Id int64 `xorm:"pk autoincr"`
	Name string
	Duration int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type StationBFabulous struct {
	Id int64 `xorm:"pk autoincr"`
	StationBId int64
	VideoId int64
	Type int
	CreatedAt time.Time
	UpdatedAt time.Time
}

type StationBCollection struct {
	Id int64 `xorm:"pk autoincr"`
	StationBId int64
	VideoId int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

type StationBPublish struct {
	Id int64 `xorm:"pk autoincr"`
	StationBId int64
	VideoUrl string
	PublishVideoId int64
	PublishVideo *Video `xorm:"-"`
	Describe string
	IsForward bool
	CreatedAt time.Time
	UpdatedAt time.Time
}
type StationBFollow struct {
	Id int64 `xorm:"pk autoincr"`
	StationBId int64
	BeNoticedId int64
	CreatedAt time.Time
	UpdatedAt time.Time
}

func QueryVideo(sbId int64, visitor int64)([]*VideoCover, error){

	var vcList []*VideoCover
	//x.Where("station_b_id = ?", sbId).Find(&vcList)
	x.Find(&vcList)
	for _ ,vc := range vcList {
		x.Where("video_cover_id = ?", vc.Id).Find(&vc.VideoList)
		for _, v := range vc.VideoList {
			x.Where("video_id = ?", v.Id).Find(&v.CommentList)

			var sf StationBFabulous
			x.Where("station_b_id = ?", sbId).And("video_id = ?", visitor).Get(&sf)
			if sf.Id != 0{
				v.Fabulous = true
				v.FabulousType = sf.Type
			}

			var sc StationBCollection
			x.Where("station_b_id = ?", sbId).And("video_id = ?", visitor).Get(&sc)
			if sf.Id != 0{
				v.Collection = true
			}

			var sp StationBPublish
			x.Where("station_b_id = ?", sbId).And("is_forward = ?", true).And("publish_video_id = ?", visitor).Get(&sp)
			if sf.Id != 0{
				v.Forward = true
			}

			var s StationB
			x.Id(sbId).Get(&s)

			var vc VideoCover
			x.Id(v.VideoCoverId).Get(&vc)

			if s.CommentMicroblog == 3 {
				var sbf StationBFollow
				x.Where("station_b_id = ?", sbId).And("be_noticed_id = ?", vc.StationBId).Get(&sbf)
				if sbf.Id != 0{
					v.CommentButton = true
				}
			}else if s.CommentMicroblog == 2 {
				var sbf StationBFollow
				x.Where("station_b_id = ?", vc.StationBId).And("be_noticed_id = ?",sbId ).Get(&sbf)
				if sbf.Id != 0{
					v.CommentButton = true
				}
			}

			for _, c := range v.CommentList{
				x.Where("comment_id = ?", v.Id).Find(&c.ReplyList)
			}
		}
	}
	return vcList, nil
}

func BuyingMember(sbId int64, mp *MemberPackage)(map[string]interface{}, error){

	var s StationB
	s.Member = true
	s.ExpirationTime = time.Now().AddDate(0, mp.Duration, 0)
	x.Id(sbId).Update(&s)

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func QueryFollowList(sbId int64)([]*StationB, error){

	var sList []*StationB
	x.Join("INNER", "station_b_follow", "station_b_follow.be_noticed_id = station_b.id").Where("station_b_follow.station_b_id = ?", sbId).Find(&sList)

	return sList, nil
}

func queryPublish(sbId int64)  {
	var spList []*StationBPublish
	x.Where("station_b_id = ?", sbId).Find(&spList)
	for _, sp := range spList {
		if sp.IsForward == true {
			var v Video
			x.Where("id = ?", sp.PublishVideoId).Get(&v)
			sp.PublishVideo = &v
		}
	}

}

func Renew(sbId int64, mp MemberPackage)(map[string]interface{}, error){

	var s StationB
	x.Id(sbId).Get(&s)

	if s.Member{
		s.ExpirationTime = s.ExpirationTime.AddDate(0, mp.Duration, 0)
		x.Id(s.Id).Update(&s)
	}

	return map[string]interface{}{"message": "成功","code":200}, nil
}

func ReplyMessage(cId int64, sbId int64, sbName string, rsbId int64, rsbName string, ct string)(map[string]interface{}, error){

	var r = Reply{
		CommentId:           cId,
		StationBId:          sbId,
		StationBName:        sbName,
		ReceiveStationBId:   rsbId,
		ReceiveStationBName: rsbName,
		Content:             ct,
	}
	x.Insert(&r)

	return map[string]interface{}{"message": "成功","code":200}, nil
}











